package com.sunjob.loginserver.controller;

import com.sunjob.common.constant.MROConstant;
import com.sunjob.common.result.Result;
import com.sunjob.common.util.MROImage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Wangzurong
 * @create 2022-08-19 15:12
 * 图片验证码
 */
@RestController
@Api(value = "获取图片验证码",tags = "获取图片验证码")
public class ImageCodeController {
    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    @ApiOperation("获取图片验证码")
    @GetMapping("/getCode")
    public Result getCode(HttpServletResponse response, HttpServletRequest request) throws Exception{
        MROImage mroImage = new MROImage();
        BufferedImage image = mroImage.getImage();
        // 使用ip作为键
        String remoteAddr = request.getRemoteAddr();
        ValueOperations<String, Object> stringObjectValueOperations = redisTemplate.opsForValue();
        stringObjectValueOperations.set(MROConstant.CODES+":"+remoteAddr,mroImage.getText(),300, TimeUnit.SECONDS);
        MROImage.output(image,response.getOutputStream());
        return Result.fail("生成验证码失败");
    }

}
